﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RVEAd.Business;
using RVEAd.Business.Model;

namespace RVEAd.Forms
{
    public partial class FormCatalogCategorii : Form
    {
        private RveBL rveBl;

        public FormCatalogCategorii()
        {
            InitializeComponent();
            this.Load += new EventHandler(FormCatalogCategorii_Load);
        }

        void FormCatalogCategorii_Load(object sender, EventArgs e)
        {
            rveBl = new RveBL();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.CellDoubleClick += new DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);

            LoadCategories();
        }

        void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ShowAddEditForm(true);
        }

        private void LoadCategories()
        {
            dataGridView1.DataSource = rveBl.GetAllCategories();
        }

        private void btnAdauga_Click(object sender, EventArgs e)
        {
            ShowAddEditForm(false);
        }

        private ClientCategory GetSelectedCategory()
        {
            if (dataGridView1.CurrentRow != null)
            {
                return (ClientCategory)dataGridView1.CurrentRow.DataBoundItem;
            }

            return null;
        }

        private void ShowAddEditForm(bool isEditMode)
        {
            Forms.FormAddEditCategory frmAddEditCategory = new FormAddEditCategory();

            if (isEditMode)
            {
                frmAddEditCategory.IsEditMode = true;
                frmAddEditCategory.Category = GetSelectedCategory();
            }
            else
            {
                frmAddEditCategory.IsEditMode = false;
            }

            frmAddEditCategory.FormClosed += (oo, ee) =>
            {
                LoadCategories();
            };
            frmAddEditCategory.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            ShowAddEditForm(true);
        }

        private void btnSterge_Click(object sender, EventArgs e)
        {
            var selectedCategory = GetSelectedCategory();
            if (selectedCategory != null)
            {
                if (MessageBox.Show("Doresti sa sterg randul selectat ?", "RVE", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    try
                    {
                        RveBL rveBL = new RveBL();
                        rveBL.DeleteCategory(selectedCategory.ID);

                        LoadCategories();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
